package com.sun.data_sort;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;

public class SortReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
    IntWritable index = new IntWritable(1);
    // 记录数字的索引值
    static int i = 1;

    @Override
    protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context)
            throws IOException, InterruptedException {
        // 因为存在重复的数字，会将其变成一个key 这会导致输出的结果的数字会缺少
        // values记录了所有的数字的个数，循环遍历即可解决此问题
        for (IntWritable value : values) {
            index.set(i);
            context.write(index, key);
            // 索引数加1
            i++;
        }
    }
}
